package com.study.client.common.util;

import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import java.nio.charset.StandardCharsets;

public class Sm4Util {

    //key必须是16字节，即128位
    final static String key = "ems1649816107900";
    static SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", key.getBytes(StandardCharsets.UTF_8));

    //加密 sm4+base64
    public static String encryptSm4Base64(String plaintext) {
        return sm4.encryptBase64(plaintext.getBytes(StandardCharsets.UTF_8));
    }

    //解密 base64+sm4
    public static String decryptSm4Base64(String ciphertext) {
        return sm4.decryptStr(Base64.decode(ciphertext),StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        String content = "i am ems";
        String plain = encryptSm4Base64(content);
        String cipher = decryptSm4Base64(plain);
        System.out.println(plain + "\n" + cipher);
    }
}
